<?php
Class PhenotypeDAO
{
	public $connexion;

	public function __construct()
	{
		try {
			$this->connexion = new PDO("mysql:host=".SERVEUR_MYSQL.";dbname=".BASE.";charset=utf8", UTILISATEUR, M2P);
		}
		catch(PDOException $e)
		{
			echo "Erreur : ".$e->getMessage();
			die();
		}
	}
	
	public function rechercher_phenoByPere($idPhenotypePere = null)
	{
		$query = "SELECT * "
				."FROM phenotype WHERE 1 ";
		if ($idPhenotypePere != null){
			$query .= " AND idphenotypepere = :idPhenotypePere ";
		}
		$query .= " ORDER BY idphenotype ";
		$requette_preparee = $this->connexion->prepare($query);
		if ($idPhenotypePere != null){
			$requette_preparee->bindValue(":idPhenotypePere" , $idPhenotypePere, PDO::PARAM_INT);
		}
		$requette_preparee->execute();
		$phenos = $requette_preparee->fetchAll(PDO::FETCH_CLASS,'Phenotype');
		return  $phenos;
	
	} // rechercher_phenoByPere
	public function rechercher_phenoByIdPhenotype($idPhenotype = null)
	{
		$query = "SELECT * "
				."FROM phenotype WHERE 1 ";
		if ($idPhenotype != null){
			$query .= " AND idphenotype = :idphenotype ";
		}
	
		$requette_preparee = $this->connexion->prepare($query);
		if ($idPhenotype != null){
			$requette_preparee->bindValue(":idphenotype" , $idPhenotype, PDO::PARAM_INT);
		}
		$requette_preparee->execute();
		$pheno = $requette_preparee->fetchObject('Phenotype');
		return  $pheno;
	
	} // rechercher_phenoByIdPhenotype
	
	public function rechercher_phenoByIdHPO($idHPO = null)
	{
		$query = "SELECT * "
				."FROM phenotype WHERE 1 ";
		if ($idHPO != null){
			$query .= " AND idHPO = :idHPO ";
		}
	
		$requette_preparee = $this->connexion->prepare($query);
		if ($idHPO != null){
			$requette_preparee->bindValue(":idHPO" , $idHPO, PDO::PARAM_INT);
		}
		$requette_preparee->execute();
		$pheno = $requette_preparee->fetchObject('Phenotype');
		return  $pheno;
	
	} // rechercher_phenoByIdHPO
	
	
	public function rechercher_phenotype_combo()
	{
		$query = "SELECT p.*, pp.libelle as pere "
				."FROM phenotype as p, phenotype_pere as pp WHERE 1 ";
		
		$query .= " AND pp.idphenotypepere = p.idphenotypepere ";
		$query .= " ORDER BY pp.idphenotypepere, p.idphenotype ";
		$requette_preparee = $this->connexion->prepare($query);
		$requette_preparee->execute();
		$phenos = $requette_preparee->fetchAll(PDO::FETCH_ASSOC);
		return  $phenos;
	
	} // rechercher_phenotype_combo
	
	
	public function rechercher_phenotypeByDossier($idDossier )
	{
		$query = "SELECT f.*,  fd.precision "
				."FROM phenotype as f,  phenotype_dossier as fd WHERE 1 ";
	
		$query .= " AND f.idphenotype = fd.idphenotype ";
		$query .= " AND fd.iddossier = :idDossier ";
		$query .= " ORDER BY fd.idphenotype ";
		$requette_preparee = $this->connexion->prepare($query);
		$requette_preparee->bindValue(":idDossier" , $idDossier, PDO::PARAM_INT);
		$requette_preparee->execute();
		$phenos = $requette_preparee->fetchAll(PDO::FETCH_ASSOC);
		return  $phenos;
	
	} // rechercher_phenotypeByDossier
	
	
	public function max_idPhenotype(){
		$query = "SELECT max(idphenotype) "
				."FROM phenotype WHERE 1 ";
		$requette_preparee = $this->connexion->prepare($query);
		$requette_preparee->execute();
		return $requette_preparee->fetchColumn();
	}
	
	
	public function  ajouter_phenotype_dossier ($phenotypeDossier){
		
		
		$query2 = "INSERT INTO phenotype_dossier VALUES (:idDossier , :idPhenotype , :precision )";
		$requette_preparee2 = $this->connexion->prepare($query2);
		$requette_preparee2->bindValue(":idDossier" , $phenotypeDossier->iddossier, PDO::PARAM_INT);
		$requette_preparee2->bindValue(":idPhenotype" , $phenotypeDossier->idphenotype, PDO::PARAM_INT);
		if ($phenotypeDossier->precision != null){
			$requette_preparee2->bindValue(":precision" , $phenotypeDossier->precision, PDO::PARAM_STR);
		} else  {
			$requette_preparee2->bindValue(":precision" , NULL, PDO::PARAM_NULL);
		}
		$t =$requette_preparee2->execute();
		if(!$t)
		{
				
			$taberr = $requette_preparee2->errorInfo();
			$message = "Erreur numéro :".$taberr[0]."(".$taberr[1].").$taberr[2]";
		
		} else {
			$message = $this->connexion->lastInsertId();
		}
		return $message;
	}
	public function  supprimer_phenotype_dossier ($iddossier){
		$query = "DELETE FROM phenotype_dossier WHERE iddossier = : idDossier";
		$requette_preparee = $this->connexion->prepare($query);
		$requette_preparee->bindValue(":idDossier" , $iddossier, PDO::PARAM_INT);
		$requette_preparee->execute();
	}
} // PhenotypeDAO



?>
